package com.yili.oa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yili.oa.entity.FtAccountDetailsSearch;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.entity.TableBase;
import com.yili.oa.models.Admin;
import com.yili.oa.models.FtAccountDetails;
import com.yili.oa.service.AdminService;
import com.yili.oa.service.FtAccountDetailsService;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.vo.response.FtAccountDetailsRes;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FtAccountDetailsController {

    @Autowired
    FtAccountDetailsService service;

    @Autowired
    private AdminService adminService;

    /**
     * 账户明细
     * @param searchBase
     * @return
     */
    @RequestMapping("ftAccountDetails/get")
    @ResponseBody
    public TableBase<FtAccountDetailsRes> get(FtAccountDetailsSearch searchBase) {
        SearchBase search = new SearchBase();
        Admin user = (Admin) SecurityUtils.getSubject().getSession().getAttribute("admin");
        List<Admin> adminList = new ArrayList<>();
        List<Long> ids = new ArrayList<>();
        if (user.getIsAdmin().equals(1)){
            ids = null;
            adminList = adminService.getList(search,null);
        }else {
            adminList = adminService.getList(search, user.getUsername());
        }
        if(ListUtils.isNotNullOrEmpty(ids)){
            for (Admin admin : adminList) {
                String username = admin.getUsername();
                ids.add(Long.valueOf(username));
            }
        }
        searchBase.setIds(ids);
        PageHelper.startPage(searchBase.getPage(), searchBase.getLimit());
        List<FtAccountDetailsRes> list = service.get(searchBase);
        PageInfo pageInfo = new PageInfo<>(list, searchBase.getLimit());
        TableBase<FtAccountDetailsRes> table = new TableBase<>();
        table.setCode(0);
        table.setData(list);
        table.setMsg("");
        table.setCount(pageInfo.getTotal());
        return table;
    }


    @RequestMapping("ftAccountDetails/add")
    @ResponseBody
    public Map<String,String> add(FtAccountDetails ftAccountDetails) {
        Map<String, String> map = new HashMap<>();
        service.add(ftAccountDetails);
        long id = ftAccountDetails.getId();
        System.out.println(id);
        map.put("result", "success");
        map.put("id", id+"");
        return map;
    }



}
